package com.agri.service.impl;

import com.agri.entity.CropCategory;
import com.agri.mapper.CropCategoryMapper;
import com.agri.result.Result;
import com.agri.service.CropCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 农产品分类服务实现类
 * 处理农产品分类相关的业务逻辑
 *
 * @author Agri-Fin-Sale Team
 * @since 2025-11-05
 */
@Service
public class CropCategoryServiceImpl implements CropCategoryService {
    @Autowired
    private CropCategoryMapper cropCategoryMapper;

    @Override
    public Result<?> listCategories() {
        QueryWrapper<CropCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1) // 只查询启用状态的分类
               .orderByAsc("level")
               .orderByAsc("category_code");
        return Result.success(cropCategoryMapper.selectList(wrapper));
    }

    @Override
    public Result<?> getCategoryById(Long id) {
        CropCategory category = cropCategoryMapper.selectById(id);
        if (category == null) {
            return Result.fail("分类不存在");
        }
        return Result.success(category);
    }

    @Override
    public Result<?> getCategoriesByParent(Long parentId) {
        QueryWrapper<CropCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId)
               .eq("status", 1) // 只查询启用状态的分类
               .orderByAsc("category_code");
        return Result.success(cropCategoryMapper.selectList(wrapper));
    }

    @Override
    public Result<?> getLevel1Categories() {
        QueryWrapper<CropCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("level", 1)
               .eq("status", 1) // 只查询启用状态的分类
               .orderByAsc("category_code");
        return Result.success(cropCategoryMapper.selectList(wrapper));
    }
}